package com.hrm.controller.org;

import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.hrm.model.dto.org.OrgCountDTO;
import com.hrm.model.dto.org.OrgViewDTO;
import com.hrm.model.service.OrgService;

public class OrgMemberCountController extends AbstractController{
	
	private OrgService orgService;

	public void setOrgService(OrgService orgService) {
		this.orgService = orgService;
	}

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest arg0,
			HttpServletResponse arg1) throws Exception {
		String orgcode = arg0.getParameter("code");
		String sdate = arg0.getParameter("sdate");
		orgcode = (orgcode==null)? "01000000":orgcode;
		String p = arg0.getParameter("p");
		p = (p==null) ? "1" : p;
		String orgofchief = (p.equals("1"))? "1":null ;
		if(sdate==null){
			Calendar cal = Calendar.getInstance();
			int year = cal.get(Calendar.YEAR);
			int month = cal.get(Calendar.MONTH)+1;
			int date = cal.get(Calendar.DATE);
			sdate = (year+"-"+month+"-"+date);
		}

		String orgtree = orgService.getMenuTreeString(sdate).toString(); 
		OrgViewDTO vdto = new OrgViewDTO();
		vdto.setOrgcode(orgcode);
		vdto.setOrgofchief(orgofchief);
		OrgCountDTO countdto = orgService.getOrgCount(vdto);
		List<OrgViewDTO> orgperson = orgService.getOrgView(vdto);
		String orgname = orgService.getOrgbase(orgcode).getOrgname();
				
		ModelAndView mv = new ModelAndView();
		mv.addObject("code",orgcode);
		mv.addObject("orgname", orgname);
		mv.addObject("orgtree",orgtree);
		mv.addObject("p",p);
		mv.addObject("countdto", countdto);
		mv.addObject("sdate", sdate);
		mv.addObject("orgperson", orgperson);		
		mv.setViewName("orgmembercount");

		return mv;
	}
	
	
	

}
